using System.Text.Json.Serialization;

namespace Uantek.Device.Communication.Models
{
    /// <summary>
    /// 设备状态枚举
    /// </summary>
    public enum DeviceConnectionStatus
    {
        /// <summary>
        /// 未连接
        /// </summary>
        Disconnected = 0,

        /// <summary>
        /// 正在连接
        /// </summary>
        Connecting = 1,

        /// <summary>
        /// 已连接
        /// </summary>
        Connected = 2,

        /// <summary>
        /// 连接失败
        /// </summary>
        Failed = 3,

        /// <summary>
        /// 连接超时
        /// </summary>
        Timeout = 4,

        /// <summary>
        /// 正在断开
        /// </summary>
        Disconnecting = 5
    }

    /// <summary>
    /// 设备运行状态枚举
    /// </summary>
    public enum DeviceOperationStatus
    {
        /// <summary>
        /// 停止
        /// </summary>
        Stopped = 0,

        /// <summary>
        /// 运行中
        /// </summary>
        Running = 1,

        /// <summary>
        /// 暂停
        /// </summary>
        Paused = 2,

        /// <summary>
        /// 故障
        /// </summary>
        Fault = 3,

        /// <summary>
        /// 维护中
        /// </summary>
        Maintenance = 4,

        /// <summary>
        /// 未知
        /// </summary>
        Unknown = 5
    }

    /// <summary>
    /// 设备状态模型
    /// </summary>
    public class DeviceStatus
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        [JsonPropertyName("deviceId")]
        public string DeviceId { get; set; } = string.Empty;

        /// <summary>
        /// 设备名称
        /// </summary>
        [JsonPropertyName("deviceName")]
        public string DeviceName { get; set; } = string.Empty;

        /// <summary>
        /// 连接状态
        /// </summary>
        [JsonPropertyName("connectionStatus")]
        public DeviceConnectionStatus ConnectionStatus { get; set; } = DeviceConnectionStatus.Disconnected;

        /// <summary>
        /// 运行状态
        /// </summary>
        [JsonPropertyName("operationStatus")]
        public DeviceOperationStatus OperationStatus { get; set; } = DeviceOperationStatus.Unknown;

        /// <summary>
        /// 最后通信时间
        /// </summary>
        [JsonPropertyName("lastCommunicationTime")]
        public DateTime? LastCommunicationTime { get; set; }

        /// <summary>
        /// 连接时间
        /// </summary>
        [JsonPropertyName("connectionTime")]
        public DateTime? ConnectionTime { get; set; }

        /// <summary>
        /// 连接信息
        /// </summary>
        [JsonPropertyName("connectionInfo")]
        public string ConnectionInfo { get; set; } = string.Empty;

        /// <summary>
        /// 协议名称
        /// </summary>
        [JsonPropertyName("protocolName")]
        public string ProtocolName { get; set; } = string.Empty;

        /// <summary>
        /// 传输类型
        /// </summary>
        [JsonPropertyName("transportType")]
        public string TransportType { get; set; } = string.Empty;

        /// <summary>
        /// 数据收集状态
        /// </summary>
        [JsonPropertyName("isCollecting")]
        public bool IsCollecting { get; set; } = false;

        /// <summary>
        /// 数据收集间隔（毫秒）
        /// </summary>
        [JsonPropertyName("collectionInterval")]
        public int CollectionInterval { get; set; } = 1000;

        /// <summary>
        /// 总通信次数
        /// </summary>
        [JsonPropertyName("totalCommunications")]
        public long TotalCommunications { get; set; } = 0;

        /// <summary>
        /// 成功通信次数
        /// </summary>
        [JsonPropertyName("successfulCommunications")]
        public long SuccessfulCommunications { get; set; } = 0;

        /// <summary>
        /// 失败通信次数
        /// </summary>
        [JsonPropertyName("failedCommunications")]
        public long FailedCommunications { get; set; } = 0;

        /// <summary>
        /// 通信成功率（百分比）
        /// </summary>
        [JsonPropertyName("successRate")]
        public double SuccessRate => TotalCommunications > 0 ? (double)SuccessfulCommunications / TotalCommunications * 100 : 0;

        /// <summary>
        /// 最后错误信息
        /// </summary>
        [JsonPropertyName("lastError")]
        public string? LastError { get; set; }

        /// <summary>
        /// 最后错误时间
        /// </summary>
        [JsonPropertyName("lastErrorTime")]
        public DateTime? LastErrorTime { get; set; }

        /// <summary>
        /// 设备版本信息
        /// </summary>
        [JsonPropertyName("deviceVersion")]
        public string? DeviceVersion { get; set; }

        /// <summary>
        /// 设备序列号
        /// </summary>
        [JsonPropertyName("serialNumber")]
        public string? SerialNumber { get; set; }

        /// <summary>
        /// 扩展状态信息
        /// </summary>
        [JsonPropertyName("extendedStatus")]
        public Dictionary<string, object> ExtendedStatus { get; set; } = new();

        /// <summary>
        /// 状态更新时间
        /// </summary>
        [JsonPropertyName("statusUpdateTime")]
        public DateTime StatusUpdateTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 是否已连接（兼容性属性）
        /// </summary>
        [JsonPropertyName("isConnected")]
        public bool IsConnected
        {
            get => ConnectionStatus == DeviceConnectionStatus.Connected;
            set
            {
                if (value)
                    ConnectionStatus = DeviceConnectionStatus.Connected;
                else
                    ConnectionStatus = DeviceConnectionStatus.Disconnected;
            }
        }

        /// <summary>
        /// 最后更新时间（兼容性属性）
        /// </summary>
        [JsonPropertyName("lastUpdateTime")]
        public DateTime LastUpdateTime
        {
            get => StatusUpdateTime;
            set => StatusUpdateTime = value;
        }

        /// <summary>
        /// 状态描述（兼容性属性）
        /// </summary>
        [JsonPropertyName("status")]
        public string Status
        {
            get => $"{ConnectionStatus}";
            set
            {
                if (Enum.TryParse<DeviceConnectionStatus>(value, out var status))
                    ConnectionStatus = status;
            }
        }

        /// <summary>
        /// 错误信息（兼容性属性）
        /// </summary>
        [JsonPropertyName("errorMessage")]
        public string? ErrorMessage
        {
            get => LastError;
            set => LastError = value;
        }

        /// <summary>
        /// 协议版本（兼容性属性）
        /// </summary>
        [JsonPropertyName("protocolVersion")]
        public string? ProtocolVersion
        {
            get => DeviceVersion;
            set => DeviceVersion = value;
        }
    }
}